package com.qzf.test.utils;

import com.qzf.test.model.Brand;
import com.qzf.test.model.Cell;
import com.qzf.test.model.Layer;
import com.qzf.test.model.Map;

import java.awt.*;

public class MapUtils {

    public static Map builderMap(Integer floorHeight){
        Map m = new Map();
        m.setFloorHeight(floorHeight);
        Layer l1=LayerUtils.builderLayer(6,6);
        Layer l2=LayerUtils.builderLayer(6,6);
        Layer l3=LayerUtils.builderLayer(6,6);
        Layer l4=LayerUtils.builderLayer(6,6);
        Layer l5=LayerUtils.builderLayer(6,6);
        Layer l6=LayerUtils.builderLayer(6,6);

        //建立图层关系
        l6.setParent(l5);
        l5.setParent(l4);
        l4.setParent(l3);
        l3.setParent(l2);
        l2.setParent(l1);
        //没有上一级
        l1.setParent(null);

        m.getList().add(l1);
        m.getList().add(l2);
        m.getList().add(l3);
        m.getList().add(l4);
        m.getList().add(l5);
        m.getList().add(l6);

        return m;
    }

    public  static boolean compare(Brand brand, Layer layer) {
        if (layer != null) {
            Cell[][] cells = layer.getCells();
            for (int row = 0; row < cells.length; row++) {
                for (int col = 0; col < cells[row].length; col++) {
                    Cell cell = cells[row][col];
                    if (cell.getState() == 1) {
                        Rectangle temp = cell.getBrand().getBounds();
                        Rectangle rect = brand.getBounds();
                        boolean result = rect.intersects(temp);
                        if (result) {
                            return result;
                        }
                    }
                }
            }
            if (brand.getParent() != null) {
                return compare(brand, layer.getParent());
            } else {
                return false;
            }
        }
    return false;
    }
}
